<?php

/**
 * @copyright       : FOSP
 * @version         : 20120322
 * @todo            : Table ranking controller
 * @name            : RankingController
 * @author          : QuangTM
 */
class RankingController extends Zend_Controller_Action
{

    /**
     * Action show table ranking detail by season and league
     * @author QuangTM
     */
    public function doibongAction()
    {
        $leagueId = intval($this->_request->getParam('lid', 1));
        $seasonId = intval($this->_request->getParam('sid', 0));
        
        // Get models instance
        $teamSeasonModel = TeamSeason::getInstance();
        $seasonModel = Season::getInstance();
        $leagueModel = League::getInstance();
        
        if (!$seasonId)
        {
            // Get season by league
            $arrSeasonByLeague = $seasonModel->getListSeasonByLeagueID($leagueId);
            
            // Get league detail
            $leagueDetail = $leagueModel->getListLeagueByIDs(array($leagueId));
            
            // Get link to redirect
            $direct = BASE_URL . '/bang-xep-hang/' . Fpt_Filter::setSeoLink($leagueDetail[0]['Name']) . '-' . Fpt_Filter::setSeoLink($arrSeasonByLeague[0]['NameSeason']) . '-' . $leagueId . '-' . $arrSeasonByLeague[0]['SeasonID'] . '.html';
            
            // Redirect
            $this->_redirect($direct);
            exit;
        }

        // Get detail table ranking by league and season
        $tableRanking = $teamSeasonModel->getListTableRanking($seasonId, $leagueId);

        // Get max update time
        $arrUpdateTime = array();
        
        foreach ($tableRanking as $ranking)
        {
            array_push($arrUpdateTime, $ranking['UpdateTime']);
        }


        // Get all league
        $allLeague = $leagueModel->getAll();
        
        // Get league to json
        $arrLeagues = array();
        foreach ($allLeague as $league)
        {
            $arrLeagues[$league['LeagueID']] = Fpt_Filter::setSeoLink($league['Name']);
        }
        

        // Get league detail
        $leagueDetail = $leagueModel->getListLeagueByIDs(array($leagueId));

        // Get season by league
        $arrSeasonByLeague = $seasonModel->getListSeasonByLeagueID($leagueId);
        
        $arrSeasons = array();
        foreach ($arrSeasonByLeague as $season)
        {
            $arrSeasons[$season['SeasonID']] = Fpt_Filter::setSeoLink($season['NameSeason']);
        }

        // Get season detail
        $seasonDetail = $seasonModel->getListSeasonByIDs(array($seasonId));
        
        //Append JS
        $this->view->headScript()->appendFile($this->view->config['js'] . '/table_ranking.js');

        // Assign to view
        $this->view->assign('tableRanking', $tableRanking);
        $this->view->assign('allLeague', $allLeague);
        $this->view->assign('arrLeagueJson', Zend_Json::encode($arrLeagues));
        $this->view->assign('leagueDetail', $leagueDetail[0]);
        $this->view->assign('arrSeasonByLeague', $arrSeasonByLeague);
        $this->view->assign('arrSeasonJson', Zend_Json::encode($arrSeasons));
        $this->view->assign('seasonDetail', $seasonDetail[0]);
        $this->view->assign('leagueID', $leagueId);
        $this->view->assign('seasonID', $seasonId);
        if (is_array($arrUpdateTime) && count($arrUpdateTime))
            $this->view->assign('maxUpdateTime', max($arrUpdateTime));
        else
            $this->view->assign('maxUpdateTime', NULL);
    }

}